package com.codemonkey.lq.qd.builder;

import java.util.Collection;

import com.codemonkey.lq.qd.facotry.config.SConfiguration;


/**
 * @author LQ
 */
public abstract class BaseBuilder {
	protected final SConfiguration configuration;
	
	public BaseBuilder(SConfiguration configuration) {
	    this.configuration = configuration;
	  }
	
	public Object parse(){
		Collection<BuilderPostProcessor> bppGroup = configuration.getBuilderPostProcessorGroup();
		for (BuilderPostProcessor bpp : bppGroup) {
			bpp.postProcessBeforeBuild(this, configuration);
		}
		
		Object obj = doParse();
		
		for (BuilderPostProcessor bpp : bppGroup) {
			bpp.postProcessAfterBuild(this,obj, configuration);
		}
		
		//触发解析完毕的事件
		//这里有个问题;因为我们是在基类中触发的事件,所以无法像Spring中的ReaderEventListener那样触发特定的事件;
		//而只能让子类根据相应的BaseBuilder类型来自己判断
		configuration.getBuilderEventListener().builded(this, obj, configuration);
		
		return obj;
	}	
	/**
	 * TODO 考虑加入 throws xmlParseException
	 * @return
	 */
	protected abstract Object doParse();
}
